package com.ssm.zhihu_ssm.service.impl;


import com.ssm.zhihu_ssm.dao.UserDAO;
import com.ssm.zhihu_ssm.entity.User;
import com.ssm.zhihu_ssm.entity.UserMessage;
import com.ssm.zhihu_ssm.service.UserMessageService;
import com.ssm.zhihu_ssm.service.UserService;
import com.ssm.zhihu_ssm.util.Md5;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDAO userMapper;
    @Resource
    private UserMessageService userMessageService;

    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public String loginUser(String phone, String password) {
        User user = userMapper.loginUser(phone);
        if (user == null){
            return  "202";
        }else {
            String psw = Md5.JM(user.getPassword());
            if(password.equals(psw))
            {
                return  "200";
            }
            else {
                user = null;
                return  "201";
            }
        }

    }

    @Override
    public boolean applyUser(String phone, String password) {
        String psw = Md5.KL(password);
        User user = userMapper.loginUser(phone);
        if (user!=null)
        {
            return false;
        }else {
            User applyuser = new User();
            applyuser.setPassword(psw);
            applyuser.setPhone(phone);
            userMapper.applyUser(applyuser);

            //设置个人信息
            User users = userMapper.loginUser(phone);
            String nickName = "第"+users.getId()+"位用户";
            UserMessage userMessage = new UserMessage(users.getId(),"男",nickName,null,null,null,null,null,null,null);
            userMessageService.setUserMessage(userMessage);

            return true;
        }

    }

    @Override
    public boolean findNickname(String nickname) {
        UserMessage userMessage=  userMapper.findUsernickName(nickname);
        if ( userMessage!= null)
        {
            return  true;
        }else {
            return  false;
        }
    }

    @Override
    public int findId(String phone) {
        return userMapper.findId(phone);
    }

    @Override
    public boolean setpassword(String password, int id) {
        String psw = Md5.KL(password);
        User user = new User();
        user.setPassword(psw);
        user.setId(id);
        userMapper.setpassword(user);
        return true;
    }

    @Override
    public String loginAdmin(String phone, String password) {
        User user = userMapper.loginAdmin(phone);
        if (user == null){
            return  "202";
        }else {
            String psw = Md5.JM(user.getPassword());
            if(password.equals(psw))
            {
                return  "200";
            }
            else {
                user = null;
                return  "201";
            }
        }
    }

    @Override
    public boolean delectUser(int id) {
        userMapper.delectUser(id);
        return true;
    }


}
